/*
 * 1522. 短网址 II
 * 实现一个顾客短网址，使得顾客可以创立他们自己的短网址
 * https://www.lintcode.com/problem/tiny-url-ii/description
 * 
 * 样例
 * createCustom("http://www.lintcode.com/", "lccode")
 * >> http://tiny.url/lccode
 * createCustom("http://www.lintcode.com/", "lc")
 * >> error
 * 
 * 
 * longToShort("http://www.lintcode.com/problem/")
 * >> http://tiny.url/1Ab38c   // this is just an example, you can have you own 6 characters.
 * 
 * 
 * shortToLong("http://tiny.url/lccode")
 * >> http://www.lintcode.com/
 * shortToLong("http://tiny.url/1Ab38c")
 * >> http://www.lintcode.com/problem/
 * shortToLong("http://tiny.url/1Ab38d")
 * >> null
 * 
 * 2018.06.11 @jeyming
 */
package tiny_url_ii_1522;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class Tiny_url_ii_1522 {
	static List<String> longUrl = new ArrayList<>();
	static List<String> urlKey = new ArrayList<>();
	public static String createKey() {
		boolean judge = true;
		String result = "";
		while(judge == true) {
			judge = false;
			result = "";
			for(int i = 0; i < 6; ++i) {
				int rd = Math.random() >= 0.5 ? 1 : 0;
				if(rd == 0) {
					Random r = new Random();
					result += r.nextInt(9) + 1 + "";
				} else {
					char[] A_z = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
					Random sc = new Random();
					int sub = sc.nextInt(A_z.length);
					result += A_z[sub] + "";
				}
			}
			if(urlKey.contains(result) == true) {
				judge = true;
			}
		}
		return result;
	}

	/*
	 * @param long_url: a long url
	 * @param key: a short key
	 * @return: a short url starts with http://tiny.url/
	 */
	public static String createCustom(String long_url, String key) {
		// write your code here
//		if((long_url == null) || ((urlKey.contains(key) == true) && (longUrl.contains(long_url) == false)) || ((longUrl.contains(long_url) == true) && (urlKey.get(longUrl.indexOf(long_url)) != key))) {
////			if(((urlKey.contains(key) == true) && (longUrl.contains(long_url) == false)))
////				System.out.println(1);
////			if(((longUrl.contains(long_url) == true) && (urlKey.get(longUrl.indexOf(long_url)) != key)))
////				System.out.println(2);
////			System.out.println(3);
//			return "error";
//		}			
//		else if((urlKey.contains(key) == true) && (longUrl.contains(long_url) == true)) {
////			System.out.println(4);
//			return "http://tiny.url/" + key;
//		}
//		else {
////			System.out.println(5);
//			longUrl.add(long_url);
//			urlKey.add(key);
//			String str = "http://tiny.url/";
//			return str + key;
//		}
		if((longUrl.contains(long_url) == false) && (urlKey.contains(key) ==false)) {
			longUrl.add(long_url);
			urlKey.add(key);
			return "http://tiny.url/" + key;
		} else if((longUrl.contains(long_url) == true) && (urlKey.contains(key) ==true) && ((longUrl.get(urlKey.indexOf(key)).equals(long_url) ==true) || (urlKey.get(longUrl.indexOf(long_url)).equals(key) ==true))) {	
			return "http://tiny.url/" + key;
		} else {
			return "error";
		}
	}

	/*
	 * @param long_url: a long url
	 * @return: a short url starts with http://tiny.url/
	 */
	public static String longToShort(String long_url) {
		// write your code here
		if(longUrl.contains(long_url) == true) {
			String key = urlKey.get(longUrl.indexOf(long_url));
			return "http://tiny.url/" + key;
		} else {
			String key = createKey();
			return createCustom(long_url, key);
		}
	}

	/*
	 * @param short_url: a short url starts with http://tiny.url/
	 * @return: a long url
	 */
	public static String shortToLong(String short_url) {
		// write your code here
		String key = "";
		int num = 0;
		int cnt = 0;
		for(int i = 0; i < short_url.length(); ++i) {
			if(short_url.charAt(i) == '/'){
				++cnt;
			}	
			if(cnt == 3) {
				num = i;
				break;
			}
		}
		for(int i = num + 1; i < short_url.length(); ++i) {
			key += short_url.charAt(i);
		}
		//System.out.println(key);
		if(urlKey.contains(key) != true) {
			return null;
		} else {
			return longUrl.get( urlKey.indexOf(key) );
		}
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
//		shortToLong("http://tiny.url/lccode");
//		createKey();
		System.out.println(createCustom("http://www.facebook.com", "facebook"));
		System.out.println(createCustom("http://www.facebook.com", "facebook"));
		System.out.println("-----------------");
		System.out.println(createCustom("http://www.lintcode.com/", "lccode"));
		System.out.println(longToShort("http://www.lintcode.com/problem/"));
		System.out.println(shortToLong("http://tiny.url/lccode"));
		System.out.println(createCustom("http://www.lintcode.com/", "lc"));
		System.out.println(createCustom("http://www.lintcode.com/en/ladder/", "lccode"));
		System.out.println("-----------------");
		System.out.println(createCustom("http://www.lintcode.com/p1", "000001"));
		System.out.println(createCustom("http://www.lintcode.com/p2", "000002"));
		System.out.println(createCustom("http://www.lintcode.com/p3", "000003"));
		System.out.println(createCustom("http://www.lintcode.com/p4", "000004"));
		System.out.println(longToShort("http://www.lintcode.com/problem/"));
		System.out.println(shortToLong("http://tiny.url/000002"));
		System.out.println(shortToLong("http://tiny.url/000004"));
		System.out.println(createCustom("http://www.facebook.com", "facebook"));
		System.out.println(createCustom("http://www.facebook.com", "facebook"));
		System.out.println(createCustom("http://www.google.com", "google"));
		System.out.println(createCustom("http://www.lintcode.com", "lc"));
		System.out.println(shortToLong("http://tiny.url/lc"));
		System.out.println(shortToLong("http://tiny.url/google"));
		System.out.println(createCustom("http://www.google.com", "google"));
		System.out.println(shortToLong("http://tiny.url/facebook"));
		System.out.println(longToShort("http://www.lintcode.com"));
		System.out.println(longToShort("http://www.google.com"));
		System.out.println(longToShort("http://www.facebook.com"));
	}

}
